//schedule.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({
  data: {
    typeModalShow: false,
    backTypeIndex: 0,
    backTypeArray: ['零部件损坏', '软件问题', '控制盒问题', '寄回物品', '其他问题'],
    imgArray: [],
    hospitalId: '',
  },
  onLoad: function (options) {
    this.setData({
      hospitalId: options.hospitalId,
      reportId: app.globalData.reportId
    }) 
    // Do some initialize when page load.
  },
  showTypeModal: function () {
    this.setData({
      typeModalShow: true
    })
  },
  hideModal: function () {
    this.setData({
      typeModalShow: false
    })
  },
  chooseType: function (e) {
    this.setData({
      backTypeIndex: e.currentTarget.dataset.location,
      typeModalShow: false
    })
  },
  addImg: function (e) {
    let that = this;
    let imgArray = that.data.imgArray || []
    wx.chooseImage({
      count: 12-imgArray.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log("res>>>",res)
        imgArray = imgArray.concat(res.tempFilePaths)
        that.setData({
          imgArray
        })
        console.log('imgArray>>', that.data.imgArray)
      }
    })
  },
  deleteImg: function (e) {
    let that = this
    let deleteIndex = e.currentTarget.dataset.index
    that.data.imgArray.splice(deleteIndex, 1)
    that.setData({
      imgArray: that.data.imgArray
    })
  },
  addBack: function(imgIdArray){
    let that = this
    that.setData({
      imgIdArray
    })
    lbbService.feedBack({
        hospitalId: that.data.hospitalId,
        feedBackType: that.data.problemType,
        feedBackContext: that.data.description,
        imgId: that.data.imgIdArray,
      },function(res){
        if (res.result == 0) {
          wx.showToast({
            title: '添加成功',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          wx.showToast({
            title: '添加失败',
            image: '/images/gantan.png'
          })
        }
      })
  },
  formSubmit: function (e) {
    let that = this;
    let formData = e.detail.value
    that.setData({
      problemType: formData.problemType,
      description: formData.description,
    })
    formData.hospitalId = that.data.hospitalId
    formData.reportId = app.globalData.reportId
    //上传图片
    let imgArray = that.data.imgArray
    util.upLoadImg(app.globalData.header, imgArray, 0, [], that.addBack)
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    // Do something when page show.
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onPullDownRefresh: function () {
    // Do something when pull down
  },
  // Event handler.
})